﻿using PermissionManager.Model;
using PermissionsManager.BLL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PermissionManager.BLL
{
    public class MenuFunctionService : BaseService<Function>
    {
        public List<FunctionBase> Query()
        {
            using (var db = new PermissionManagerEntities())
            {

                var result = from a in db.Function
                             join b in db.Menu
                             on a.MenuId equals b.Id
                             select new FunctionBase()
                             {
                                 Id = a.Id,
                                 MenuName = b.Name,
                                 Name =a.Name,
                                 ControllerName=a.ControllerName, 
                                 ActionName=a.ActionName,
                                 FunctionCode =a.FunctionCode,
                             };

                return result.ToList();
            }
        }

        public bool DeleteFunction(int id)
        {
            //代码执行完，则释放资源   声明一个实例
            using (var db = new PermissionManagerEntities())
            {
                using (var tran = db.Database.BeginTransaction())
                {
                    try
                    {
                        //删除功能表
                        Function function = db.Function.FirstOrDefault(a => a.Id == id);
                        db.Function.Remove(function);
                        db.SaveChanges();

                        tran.Commit();
                    }
                    catch (Exception)
                    {
                        tran.Rollback();
                        return false;
                    }
                }
            }
            return true;
        }
    }
}
